home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / popupmen / part03 < prev   
Encoding:
Internet Message Format  |  1990-04-14  |  15.1 KB

  1. Path: wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!xanth!cs.odu.edu!Amiga-Request
  2. From: Amiga-Request@cs.odu.edu (Amiga Sources/Binaries Moderator)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v90i137: PopUpMenu 3.2 - make intuition menus into pop up menus, Part03/03
  5. Message-ID: <12192@xanth.cs.odu.edu>
  6. Date: 13 Apr 90 23:46:20 GMT
  7. Sender: news@cs.odu.edu
  8. Reply-To: d5adria@dtek.chalmers.se (Martin Adrian)
  9. Lines: 499
  10. Approved: tadguy@cs.odu.edu (Tad Guy)
  11. X-Mail-Submissions-To: Amiga@cs.odu.edu
  12. X-Post-Discussions-To: comp.sys.amiga
  13.  
  14. Submitted-by: d5adria@dtek.chalmers.se (Martin Adrian)
  15. Posting-number: Volume 90, Issue 137
  16. Archive-name: util/popupmenu-3.2/part03
  17.  
  18. #!/bin/sh
  19. # This is a shell archive.  Remove anything before this line, then unpack
  20. # it by saving it into a file and typing "sh file".  To overwrite existing
  21. # files, type "sh file -c".  You can also feed this as standard input via
  22. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  23. # will see the following message at the end:
  24. #        "End of archive 3 (of 3)."
  25. # Contents:  Source/OpenWindows.c
  26. # Wrapped by tadguy@xanth on Fri Apr 13 19:45:21 1990
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f 'Source/OpenWindows.c' -a "${1}" != "-c" ; then 
  29.   echo shar: Will not clobber existing file \"'Source/OpenWindows.c'\"
  30. else
  31. echo shar: Extracting \"'Source/OpenWindows.c'\" \(12815 characters\)
  32. sed "s/^X//" >'Source/OpenWindows.c' <<'END_OF_FILE'
  33. X#include "PopUpMenu.h"
  34. X
  35. X/* these must be declared here in this silly way (lattice 5.0 bug) */
  36. XIMPORT UWORD  chip GhostPattern[];
  37. XIMPORT WORD   far  NormalPattern[];
  38. XIMPORT struct Image chip MyAmigaKeyImage[];
  39. XIMPORT struct Image far MySubItemImage[];
  40. X
  41. X
  42. X/****************************************
  43. X * OpenMenuWindow()                     *
  44. X *                    *
  45. X * Input:                *
  46. X *   none                *
  47. X * Output:                *
  48. X *   return    - TRUE if window opened. *
  49. X ****************************************/
  50. X
  51. XBOOL OpenMenuWindow(MenuTop)
  52. X  WORD MenuTop;
  53. X{
  54. X/*
  55. X  IMPORT UWORD far GhostPattern[2];
  56. X  IMPORT UWORD far NormalPattern[2];
  57. X*/
  58. X  IMPORT struct Window    *const ActiveWindow;
  59. X  IMPORT struct Menu    *const Menues;
  60. X  IMPORT struct Screen    *const Screen;
  61. X  IMPORT struct RastPort      Rp;     /* Screens RastPort */
  62. X  IMPORT struct WindowData    MenuWindow;
  63. X  IMPORT const UWORD         MenuFontSize;
  64. X
  65. X  WORD    MenuWidth     = 0;
  66. X  WORD    MenuHeight    = BORDERSIZE + 1;
  67. X
  68. X /****************************************
  69. X  * Find width & height of window needed *
  70. X  ****************************************/
  71. X  {
  72. X    struct Menu *MenuPtr = Menues;
  73. X
  74. X    do {
  75. X      UWORD Length;
  76. X
  77. X      Length = TextLength(&Rp,MenuPtr->MenuName,Mystrlen(MenuPtr->MenuName));
  78. X      if (MenuPtr->Width > Length)
  79. X    Length = MenuPtr->Width;
  80. X      if (Length > MenuWidth)
  81. X    MenuWidth = Length;
  82. X      MenuHeight += MenuFontSize;
  83. X    }
  84. X    while ((MenuPtr = MenuPtr->NextMenu) != NULL);
  85. X
  86. X    MenuWidth  += (2 * BORDERSIZE + 1);
  87. X  }
  88. X /************************************
  89. X  * Position window on screen (Left) *
  90. X  ************************************/
  91. X  {
  92. X    WORD MenuLeft = Screen->MouseX - ((UWORD)MenuWidth / 2);
  93. X
  94. X    if (MenuLeft < 0)
  95. X      MenuLeft = 0;
  96. X    if (MenuLeft + MenuWidth > Screen->Width)
  97. X      MenuLeft = Screen->Width - MenuWidth;
  98. X
  99. X    MenuWindow.LeftEdge  = MenuLeft;
  100. X    MenuWindow.Width     = MenuWidth;
  101. X  }
  102. X /***********************************
  103. X  * Position window on screen (Top) *
  104. X  ***********************************/
  105. X  {
  106. X    if (MenuTop > MenuHeight) /* menues has changed */
  107. X      MenuTop = 0;
  108. X    MenuTop  = Screen->MouseY - (MenuFontSize / 2) - MenuTop;
  109. X
  110. X    if (MenuTop < 0)
  111. X      MenuTop = 0;
  112. X    if (MenuTop + MenuHeight > Screen->Height)
  113. X      MenuTop = Screen->Height - MenuHeight;
  114. X
  115. X    MenuWindow.TopEdge     = MenuTop;
  116. X    MenuWindow.Height     = MenuHeight;
  117. X  }
  118. X /*******************************
  119. X  * Open window with right size *
  120. X  *******************************/
  121. X
  122. X  if (BuildBitMap(&MenuWindow) == FALSE)
  123. X    return (FALSE);
  124. X
  125. X /**********************
  126. X  * Fill in all menues *
  127. X  **********************/
  128. X  {
  129. X    const LONG     MenuLeft = MenuWindow.LeftEdge + BORDERSIZE;
  130. X    struct Menu *MenuPtr  = Menues;
  131. X
  132. X    do {
  133. X      MenuTop += MenuFontSize;
  134. X
  135. X      SetAPen(&Rp,(LONG)ActiveWindow->DetailPen);
  136. X      Move(&Rp,MenuLeft,(LONG)MenuTop - 1);
  137. X      Text(&Rp,MenuPtr->MenuName,Mystrlen(MenuPtr->MenuName));
  138. X
  139. X      /* Ghostitem ? */
  140. X      if (NOT (MenuPtr->Flags & MENUENABLED)) {
  141. X    SetAfPt(&Rp, GhostPattern, 1);
  142. X    SetAPen(&Rp,(LONG)ActiveWindow->BlockPen);
  143. X    RectFill(&Rp,MenuLeft,
  144. X             (LONG)(MenuTop - MenuFontSize + BORDERSIZE),
  145. X             (LONG)(MenuWindow.RightEdge - BORDERSIZE),
  146. X             (LONG)MenuTop);
  147. X    SetAfPt(&Rp,NormalPattern,1);
  148. X      }
  149. X    }
  150. X    while ((MenuPtr = MenuPtr->NextMenu) != NULL);
  151. X  }
  152. X
  153. X  return (TRUE);
  154. X}
  155. X
  156. X/**************************************************************
  157. X * OpenItemWindow(ItemWindow,ParentWindow,TopPos,WindowType)  *
  158. X *                                  *
  159. X * Input:                              *
  160. X *   ItemWindow   - Window to open.                  *
  161. X *   ParentWindow -                          *
  162. X *   TopPos      - Top position for new window.          *
  163. X *   WindowType   - ITEMWINDOW or SUBWINDOW              *
  164. X * Output:                              *
  165. X *   none                              *
  166. X **************************************************************/
  167. XVOID OpenItemWindow(ItemWindow,ParentWindow,TopPos,WindowType)
  168. X  struct WindowData  *const ItemWindow;
  169. X  struct WindowData  *const ParentWindow;
  170. X  const WORD TopPos;
  171. X  const UWORD WindowType;
  172. X{
  173. X  IMPORT struct Screen       *const Screen;
  174. X  IMPORT struct WindowData  MenuWindow;
  175. X  IMPORT const WORD  MouseX;
  176. X
  177. X  struct WindowSize  Size;
  178. X
  179. X /****************************************
  180. X  * Find width & height of window needed *
  181. X  ****************************************/
  182. X  {
  183. X    struct MenuItem  *Item = ItemWindow->Items;
  184. X
  185. X    Size.Left  = WORD_MAX;
  186. X    Size.Top   = WindowType; /* (ITEMWINDOW -> TopPos >= 0), SUBWINDOW -> no limit */
  187. X    Size.Right = Size.Bottom = WORD_MIN;
  188. X
  189. X    do {
  190. X      CheckItemSize(&Size,Item,(LONG)Item->ItemFill);
  191. X      if ((Item->Flags & HIGHFLAGS) == HIGHIMAGE)
  192. X    CheckItemSize(&Size,Item,(LONG)Item->SelectFill); /* check highlighted */
  193. X    }
  194. X    while (Item = Item->NextItem);
  195. X  }
  196. X /**********************************************
  197. X  * Position window on screen (left)           *
  198. X  * Possible positions:                *
  199. X  *   1. At real position (a'la intuition).    *
  200. X  *   2. At right side of parent.           *
  201. X  *   3. At left side of parent.           *
  202. X  *   4. On the side that covers parent least. *
  203. X  **********************************************/
  204. X  {
  205. X    WORD  WindowLeft;
  206. X    WORD  LeftValue   = Size.Left - BORDERSIZE;
  207. X    UWORD WindowWidth = Size.Right - LeftValue + BORDERSIZE;
  208. X    WORD  LeftPos2    = ParentWindow->LeftEdge - WindowWidth + 7;
  209. X    WORD  MaxLeft     = Screen->Width - WindowWidth;
  210. X
  211. X    if ((WindowType == ITEMWINDOW) OR
  212. X    ((WindowLeft = ParentWindow->LeftEdge + LeftValue) > MaxLeft) OR
  213. X    (WindowLeft < 0))
  214. X      WindowLeft = ParentWindow->RightEdge - 7;
  215. X
  216. X    if (WindowLeft > MaxLeft)
  217. X      if (LeftPos2 > MaxLeft - WindowLeft)
  218. X    WindowLeft = (LeftPos2 > 0) ? LeftPos2 : 0;
  219. X      else
  220. X    WindowLeft = MaxLeft;
  221. X
  222. X    if (WindowLeft + WindowWidth < ParentWindow->LeftEdge + 7)
  223. X      WindowWidth = ParentWindow->LeftEdge + 7 - WindowLeft;
  224. X
  225. X    ItemWindow->LeftEdge  = WindowLeft;
  226. X    ItemWindow->LeftValue = LeftValue - WindowLeft;
  227. X    ItemWindow->Width      = WindowWidth;
  228. X  }
  229. X /***********************************
  230. X  * Position Window on screen (Top) *
  231. X  ***********************************/
  232. X  {
  233. X    WORD  TopValue     = Size.Top - BORDERSIZE;
  234. X    WORD  WindowTop    = TopPos + TopValue;
  235. X    UWORD WindowHeight = Size.Bottom - TopValue  + BORDERSIZE;
  236. X
  237. X    if (WindowTop + WindowHeight > Screen->Height)
  238. X      WindowTop = Screen->Height - WindowHeight;
  239. X    if (WindowTop < 0)
  240. X      WindowTop = 0;
  241. X
  242. X    ItemWindow->TopEdge   = WindowTop;
  243. X    ItemWindow->TopValue  = TopValue - WindowTop;
  244. X    ItemWindow->Height      = WindowHeight;
  245. X  }
  246. X
  247. X  if (BuildBitMap(ItemWindow) == FALSE)
  248. X    return;
  249. X
  250. X  DrawAllItems(ItemWindow);
  251. X}
  252. X
  253. X/****************************************
  254. X * DrawAllItems(ItemWindow)             *
  255. X *                    *
  256. X * Input:                *
  257. X *   ItemWindow  - Window to draw into. *
  258. X *                    *
  259. X * Output                *
  260. X *   none                *
  261. X ****************************************/
  262. XVOID DrawAllItems(ItemWindow)
  263. X  struct WindowData *const ItemWindow;
  264. X{
  265. X  struct MenuItem *Item = ItemWindow->Items;
  266. X
  267. X  ClearWindow(ItemWindow);
  268. X
  269. X  do
  270. X    DrawMenuItem(Item, ItemWindow, ITEMFILL, DONTCLEAROLD);
  271. X  while ((Item = Item->NextItem) != NULL);
  272. X}
  273. X
  274. X/****************************************************************************
  275. X * DrawMenuItem(Item, ItemWindow, Mode, Clear)                              *
  276. X *                                        *
  277. X * Input:                                    *
  278. X *   Item -      Item to draw.                         *
  279. X *   ItemWindow - Data about window to draw item into                *
  280. X *   Mode -      ITEMFILL = Draw Item                        *
  281. X *          SELECTFILL = Draw selected item (if any)                  *
  282. X *   Clear -      Clear position before drawing  (Only used with HighImage) *
  283. X * OUTPUT                                    *
  284. X *   none                                    *
  285. X ****************************************************************************/
  286. X
  287. XVOID DrawMenuItem(Item,ItemWindow,Mode,Clear)
  288. X  struct MenuItem *const Item;
  289. X  struct WindowData  *const ItemWindow;
  290. X  const UWORD Mode;
  291. X  const BOOL   Clear;
  292. X{
  293. X/*
  294. X  IMPORT UWORD far GhostPattern[2];
  295. X  IMPORT UWORD far NormalPattern[2];
  296. X  IMPORT struct Image far MyAmigaKeyImage[2];
  297. X  IMPORT struct Image far MySubItemImage[2];
  298. X*/
  299. X
  300. X  IMPORT struct RastPort Rp;
  301. X  IMPORT struct Window    *const ActiveWindow;
  302. X  IMPORT struct Screen    *const Screen;
  303. X  IMPORT const BOOL  ScreenType;
  304. X
  305. X  union FillTypes Fill;
  306. X  const LONG Left = Item->LeftEdge - ItemWindow->LeftValue;
  307. X  const LONG Right = Left + Item->Width - 1;
  308. X  const LONG Top = Item->TopEdge - ItemWindow->TopValue;
  309. X  const LONG Bottom = Top + Item->Height - 1;
  310. X
  311. X  /* Find what to Draw */
  312. X  if (!(Fill.APTR = (Mode == SELECTFILL) ? Item->SelectFill : Item->ItemFill))
  313. X    return;  /* nothing to draw */
  314. X
  315. X  SetAPen(&Rp, (LONG)(ActiveWindow->BlockPen));
  316. X  SetDrMd(&Rp, JAM1);
  317. X
  318. X  if (Clear)
  319. X    /* Erase whay may already be here */
  320. X    RectFill(&Rp, Left, Top, Right, Bottom);
  321. X
  322. X  /* Now, draw the item itself -- depending on the Flag value, it    */
  323. X  /* could be either an Image or an IntuiText */
  324. X
  325. X  if (Item->Flags & ITEMTEXT)
  326. X    PrintIText(&Rp, Fill.IText, Left, Top);
  327. X  else
  328. X    DrawImage(&Rp,  Fill.Image, Left, Top);
  329. X
  330. X  /* If the item is checkmarked, draw the checkmark. */
  331. X
  332. X  if ((Item->Flags & (CHECKIT | CHECKED)) == (CHECKIT | CHECKED))
  333. X    DrawImage(&Rp, ActiveWindow->CheckMark, Left, Top);
  334. X
  335. X  /* If the item has a commandkey, or a subitem */
  336. X  {
  337. X    const LONG ItemTop    = Top + ((Item->Flags & ITEMTEXT) ?
  338. X                     Fill.IText->TopEdge:
  339. X                     Fill.Image->TopEdge);
  340. X
  341. X    if (Item->Flags & COMMSEQ) {
  342. X      const LONG KeyLeft = Right - Rp.TxWidth;
  343. X
  344. X      DrawImage(&Rp, &MyAmigaKeyImage[ScreenType],KeyLeft, ItemTop);
  345. X
  346. X      Move(&Rp, KeyLeft, ItemTop + Rp.TxHeight - 2);
  347. X      SetAPen(&Rp, (LONG)(ActiveWindow->DetailPen));
  348. X      Text(&Rp,&Item->Command,1);
  349. X    }
  350. X
  351. X    if (Item->SubItem)
  352. X      DrawImage(&Rp,&MySubItemImage[ScreenType],Right,ItemTop);
  353. X  }
  354. X
  355. X  /* If the ITEMENABLED flag is not set, "ghost" the item. */
  356. X
  357. X  if (!(Item->Flags & ITEMENABLED)) {
  358. X    SetAfPt(&Rp, GhostPattern, 1);
  359. X    SetAPen(&Rp, (LONG)(ActiveWindow->BlockPen));
  360. X    RectFill(&Rp, Left, Top, Right, Bottom);
  361. X    SetAfPt(&Rp, NormalPattern, 1);
  362. X  }
  363. X}
  364. X
  365. X/****************************************
  366. X * BuildBitMap(Window)  - OpenWindow    *
  367. X *                    *
  368. X * Input:                *
  369. X *   Window    - Window to open.    *
  370. X * Output:                *
  371. X *   return    - TRUE if window opened. *
  372. X ****************************************/
  373. XBOOL BuildBitMap(Window)
  374. X  struct WindowData *const Window;
  375. X{
  376. X  IMPORT struct Screen     *const Screen;
  377. X  IMPORT struct RastPort Rp;
  378. X
  379. X  const LONG WindowWidth  = Window->Width;
  380. X  const LONG WindowHeight = Window->Height;
  381. X
  382. X  /* is window to big for screen ? */
  383. X
  384. X  if ((WindowWidth > Screen->Width) OR
  385. X      (WindowHeight > Screen->Height))
  386. X    return (FALSE);
  387. X
  388. X  Window->RightEdge = Window->LeftEdge + WindowWidth  - 1;
  389. X  Window->Bottom    = Window->TopEdge  + WindowHeight - 1;
  390. X
  391. X  /* init bitmap */
  392. X  {
  393. X    const ULONG Depth = Screen->BitMap.Depth;
  394. X    UWORD i;
  395. X
  396. X    InitBitMap(&Window->Bm,Depth,WindowWidth,WindowHeight);
  397. X
  398. X    /* allocate raster for all bitplanes */
  399. X
  400. X    for (i = 0; i < Depth; i++)
  401. X      if ((Window->Bm.Planes[i] = AllocRaster(WindowWidth,WindowHeight)) == NULL) {
  402. X    RemoveBitMap(Window);
  403. X    return(FALSE);
  404. X      }
  405. X  }
  406. X
  407. X  Window->BitMapOk = TRUE;
  408. X
  409. X  /* make window visible */
  410. X  SwapBits(Window);
  411. X
  412. X  ClearWindow(Window);
  413. X
  414. X  return (TRUE);
  415. X}
  416. X
  417. X/*********************************
  418. X * ClearWindow(window)           *
  419. X *                 *
  420. X * Input:             *
  421. X *   Window   - Window to clear. *
  422. X * OutPut:             *
  423. X *   none             *
  424. X *********************************/
  425. XVOID ClearWindow(Window)
  426. X  struct WindowData *const Window;
  427. X{
  428. X  IMPORT struct RastPort Rp;
  429. X  IMPORT struct Window *const ActiveWindow;
  430. X
  431. X  /* window should look like intuitionmenues */
  432. X  SetDrMd(&Rp, JAM1);
  433. X
  434. X  SetAPen(&Rp,(LONG)ActiveWindow->BlockPen);
  435. X  SetOPen(&Rp,(LONG)ActiveWindow->DetailPen);
  436. X
  437. X  RectFill(&Rp,(LONG)Window->LeftEdge,
  438. X           (LONG)Window->TopEdge,
  439. X           (LONG)Window->RightEdge,
  440. X           (LONG)Window->Bottom);
  441. X  BNDRYOFF(&Rp);
  442. X}
  443. X
  444. X/******************************
  445. X * CloseItemWindow(Window)    *
  446. X *                  *
  447. X *INPUT               *
  448. X *  Window - Window to close. *
  449. X *OUTPUT              *
  450. X *  none              *
  451. X ******************************/
  452. XVOID CloseItemWindow(Window)
  453. X  struct WindowData *const Window;
  454. X{
  455. X  if (Window->BitMapOk) {
  456. X    /* remove window from screen */
  457. X    SwapBits(Window);
  458. X
  459. X    /* remove all bitplanes */
  460. X    RemoveBitMap(Window);
  461. X  }
  462. X}
  463. X
  464. X/****************************************************************************
  465. X * RemoveBitMap(Window) - Remove allocated rasters in the BitMap structure. *
  466. X *                                        *
  467. X * Input:                                    *
  468. X *   Window   - Window with bitmap-                        *
  469. X * Output:                                    *
  470. X *   none                                    *
  471. X ****************************************************************************/
  472. XVOID RemoveBitMap(Window)
  473. X  struct WindowData *const Window;
  474. X{
  475. X  struct BitMap *const Bm = &Window->Bm;
  476. X  UWORD  i;
  477. X
  478. X  /* remove all allocated rasters */
  479. X  for (i = 0; i < Bm->Depth; i++)
  480. X    if (Bm->Planes[i])
  481. X      FreeRaster(Bm->Planes[i],(LONG)Window->Width,(LONG)Window->Height);
  482. X
  483. X  Window->BitMapOk = FALSE;
  484. X}
  485. X
  486. END_OF_FILE
  487. if test 12815 -ne `wc -c <'Source/OpenWindows.c'`; then
  488.     echo shar: \"'Source/OpenWindows.c'\" unpacked with wrong size!
  489. fi
  490. # end of 'Source/OpenWindows.c'
  491. fi
  492. echo shar: End of archive 3 \(of 3\).
  493. cp /dev/null ark3isdone
  494. MISSING=""
  495. for I in 1 2 3 ; do
  496.     if test ! -f ark${I}isdone ; then
  497.     MISSING="${MISSING} ${I}"
  498.     fi
  499. done
  500. if test "${MISSING}" = "" ; then
  501.     echo You have unpacked all 3 archives.
  502.     rm -f ark[1-9]isdone
  503. else
  504.     echo You still need to unpack the following archives:
  505.     echo "        " ${MISSING}
  506. fi
  507. ##  End of shell archive.
  508. exit 0
  509. -- 
  510. Mail submissions (sources or binaries) to <amiga@cs.odu.edu>.
  511. Mail comments to the moderator at <amiga-request@cs.odu.edu>.
  512. Post requests for sources, and general discussion to comp.sys.amiga.
  513.